"""
    Topic：bilibili 登录脚本
    Version：1.0
    Date：2021-6-17
    Info：密码登录，半自动，不能执行滑动验证,生成cookies，用于后期免密登录
"""
from selenium import webdriver
import json
import time
# 驱动直接放到当前路径下
driver = webdriver.Chrome()
#login_url = 'https://passport.bilibili.com/login?from_spm_id=333.851.top_bar.login'
login_url = 'https://gitee.com/login'  #Gitee
driver.get(url=login_url)
#输入信息
#login_user = driver.find_element_by_id('login-username')
login_user = driver.find_element_by_xpath('//*[@id="user_login"]')  #Gitee
login_user.send_keys('18354266479')
#login_pwd = driver.find_element_by_id('login-passwd')
login_pwd = driver.find_element_by_xpath('//*[@id="user_password"]')  #Gitee
login_pwd.send_keys('whq18354266479')

#点击登录
#login_btn = driver.find_element_by_class_name('btn-login')
login_btn = driver.find_element_by_xpath('//*[@id="new_user"]/div[2]/div/div/div[4]/input') #Gitee
login_btn.click()

#为了让driver.get_cookies()获得完整，特意停留，确保拿到的是登陆成功后的Cookies
time.sleep(10)
#保存Cookies
json_cookies_list = driver.get_cookies()
# print(type(cookies))
json_cookies_string = json.dumps(json_cookies_list)
with open('cookies.txt',mode='w',encoding='utf-8') as file:
    file.write(json_cookies_string)
    #冲刷缓冲区
    file.flush()


